package top.igotcha.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.igotcha.eduservice.entity.EduCourse;
import top.igotcha.eduservice.entity.vo.*;
import top.igotcha.eduservice.service.EduCourseService;
import top.igotcha.commonutils.utils.R;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-06
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @PutMapping()
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String  courseId= courseService.saveCourseInfo(courseInfoVo);
        return R.OK().data("courseId",courseId);
    }

    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable  String  courseId){
        courseService.removeCourse(courseId);
        return R.OK();
    }
    @PostMapping()
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.OK();
    }

    @GetMapping("{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo  courseInfoVo= courseService.getCourseInfo(courseId);
        return R.OK().data("courseInfoVo",courseInfoVo);
    }

    //@ApiOperation(value = "条件分页课程列表")
    //@PostMapping("{page}/{limit}")
    //public R pageTeacherCondition(
    //        @ApiParam(name = "page", value = "当前页码", required = true)
    //        @PathVariable Long page,
    //
    //        @ApiParam(name = "limit", value = "每页记录数", required = true)
    //        @PathVariable Long limit,
    //
    //        @ApiParam(name = "courseQuery", value = "查询对象", required = false)
    //        @RequestBody(required = false)
    //                CourseQueryVO vo){
    //
    //    Page<EduCourse> pageParam = new Page<>(page, limit);
    //    courseService.pageCourseCondition(pageParam, vo);
    //    List<EduCourse> records = pageParam.getRecords();
    //    long total = pageParam.getTotal();
    //    return  R.OK().data("total", total).data("rows", records);
    //}

    @ApiOperation(value = "条件分页课程列表")
    @PostMapping("{page}/{limit}")
    public R pageTeacherNewCondition(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseNewQuery", value = "查询对象", required = false)
            @RequestBody(required = false)
                    CourseNewQueryVO vo){

        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageCourseNewCondition(pageParam, vo);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.OK().data("total", total).data("rows", records);
    }



    @ApiOperation(value = "根据课程ID获取课程公示信息")
    @GetMapping("/publishCourse/{courseId}")
    public R getPublishCourse(@PathVariable String courseId){
        CoursePublishVo coursePublish= courseService.getCoursePublish(courseId);
        return R.OK().data("publishCourse",coursePublish);
    }

    @ApiOperation(value = "发布课程")
    @PutMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.OK();
    }
}

